#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <threads.h>
#include <signal.h>
#include <sys/types.h>

#define INF ((float)1.0/(float)0.0) 
#define NEG_ZERO -1.0/(1.8e308)

typedef struct s
{
    int a[2];
    double d;
} s;

typedef void handler_t(int);

double fun(int i)
{
    s x;
    x.d = 3.14;
    x.a[i] = 1073741824;
    return x.d;
}

double foo()
{
//    if(-1.0/0<1.0/0)
//        printf(">");
//    printf("%lu\n", sizeof(1.0));
//    printf("%lf\n", (double)INF);
      int x=1;
      unsigned int y=1;
      char p[100];
      memset(p, -1, 100);
      p[98]=0xfee;
      char pp = 0x8f;
      printf("%x %.2x %lu\n", p[98], pp, sizeof(p[98]));
      return 1;
}

void show_bytes(unsigned char* b, size_t len)
{
    for(int i=0; i<len;i++)
    {
        printf("%.2x ", b[i]);
    }
    printf("\n");
}

void sigtstp_handler(int sig)
{
    sigset_t mask_all, prev;
    sigfillset(&mask_all);
    sigprocmask(SIG_BLOCK, &mask_all, &prev);
    static int sigtstp_id = 0;
    char str[12] = "sigtstp-#-\n";
    str[8] = '0' + (sigtstp_id % 10);
    sigtstp_id++;
    sigprocmask(SIG_SETMASK, &prev, NULL);
    while(1)
    {
        write(1, str, 11);
        sleep(3);
    }

}

void sigint_handler(int sig)
{
    sigset_t mask_all, prev;
    sigfillset(&mask_all);
    sigprocmask(SIG_BLOCK, &mask_all, &prev);
    static int sigint_id = 0;
    char str[11] = "sigint-#-\n";
    str[7] = '0' + (sigint_id % 10);
    sigint_id++;
    sigprocmask(SIG_SETMASK, &prev, NULL);
    while(1)
    {
        write(1, str, 10);
        sleep(3);
    }

}

void* thread_routine(void *vargp)
{
    char str[11] = "thread-#-\n";
    str[7] = '0' + ((int)vargp % 10);
    while(1)
    {
        write(1, str, 10);
        sleep(3);
    }
}

int main()
{

    pthread_t tid;
    signal(SIGTSTP, sigtstp_handler);
    signal(SIGINT, sigint_handler);
    for (int i = 0; i < 3; i++)
    {
        pthread_create(&tid, NULL, thread_routine, (void *)i);
    }
    int i = 0;
    while(1)
    {
        write(1, "main thread\n", 12);
        sleep(3);
    }
    return 0;
}
